from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from .models import *
from django.db.utils import *
from .verify import *
import json
# Create your views here.


@verify_control
def add_tower_api(request):
    """
        @author:吕琛琛
        @date:2018/10/11
        本接口为增加楼房，用post方法来提交数据
        提交方式 ：POST
        提交数据格式 ：{'tower_name':'xxx'}
        @整理： 董海明
    """
    try:
        info = dict(request.POST)["add_tower_name"]
    except KeyError:
        return HttpResponse(json.dumps({"message": "提交信息格式不对，或者键不是'tower_name'"}))
    else:
        add_list = []
        for i in info:
            if i.replace(" ", "") != "":
                add_list.append(Tower(tower_name=i))
        if not add_list:
            return HttpResponse(json.dumps({"message": "楼名不能为空"}))
        try:
            Tower.objects.bulk_create(add_list)
        except IntegrityError:
            return HttpResponse(json.dumps({"message": "楼名重复"}))
        else:
            return HttpResponse(json.dumps({"message": "楼房添加成功"}))


@verify_control
def add_floor_api(request):
    """

        @author:吕琛琛
        @date:2018/10/11
        本接口为增加楼层,添加楼层需要先找到对应的楼号，如果没有，添加不会成功
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx", "floor_name": "xxx"}
        @整理：董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "没有这个楼号，请确认后再提交"}))
    # 判断同楼不可以添加同层
    else:
        try:
            Floor.objects.get(floor_num=info["floor_name"][0]).replace(" ", "")
        except Floor.DoesNotExist:
            tower_num.floor_set.create(floor_num=info["floor_name"][0])
            return HttpResponse(json.dumps({"message": "楼层添加成功！！"}))
        else:
            return HttpResponse(json.dumps({"message": "该楼层已经添加，请确认楼层"}))


@verify_control
def add_room_api(request):
    """
        @author:吕琛琛
        @date:2018/10/11
        本接口为添加房间， 添加房间需要判断楼、楼层，添加房间需要
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx", "floor_name":"xxx", "room_name":"xxx"}
        @ 整理：董海明
    """
    info = dict(request.POST)
    try:
        Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "提交的楼号不存在，请重新提交"}))
    else:
        try:
            floor_name = Floor.objects.get(floor_num=info["floor_name"][0]).replace(" ", "")
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "提交的楼层不存在，请添加楼层"}))
        else:
            try:
                room_name = Room.objects.get(room_num=info["room_name"][0]).replace(" ", "")
            except Room.DoesNotExist:
                floor_name.room_set.create(room_num=room_name)
                return HttpResponse(json.dumps({"message": "房间添加成功"}))
            else:
                return HttpResponse(json.dumps({"message": "提交的房间已经添加，不需要重复添加"}))


@verify_control
def del_tower_api(request):
    """
        @author:赵泽华
        @date:2018/10/12
        本接口为删除楼，需要检测有没有该楼,删除楼会把对应的楼层和房间删除，请谨慎
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx"}
        @整理: 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "没有这个楼号，请重新确认后提交"}))
    else:
        tower_num.delete()
        return HttpResponse(json.dumps({"message": "删除成功"}))


@verify_control
def del_floor_api(request):
    """
        @author:赵泽华
        @date:2018/10/12
        本接口为删除楼层，需要检测对应的楼，会把对应的房间删除，请谨慎
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx", "floor_name":"xxx"}
        @整理： 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0])
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "没有这个楼号，请确认后再删除"}))
    else:
        try:
            floor_name = tower_num.floor_set.get(floor_num=info["floor_name"][0])
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "您输入的楼层没有或者输入过大，请重新输入"}))
        else:
            floor_name.delete()
            return HttpResponse(json.dumps({"message": "楼层删除成功"}))


@verify_control
def del_room_api(request):
    """
        @author:赵泽华
        @date:2018/10/12
        本接口为删除房间的接口,需要检测对应的楼和楼层
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx", "floor_name":"xxx", "room_name":"xxx"}
        @整理： 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0])
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "没有这个楼号，请重新检查后输入"}))
    else:
        try:
            floor_name = tower_num.floor_set.get(floor_num=info["floor_name"][0])
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "您输入的楼层没有或者输入过大，请重新输入"}))
        else:
            try:
                room_name = floor_name.room_set.get(room_num=info["room_name"][0])
            except Room.DoesNotExist:
                return HttpResponse(json.dumps({"message": "没有这个房间，请确认后重新输入"}))
            else:
                room_name.delete()
                return HttpResponse(json.dumps({"message": "房间删除成功"}))


@verify_control
def change_tower_api(request):
    """
        @auther: 辛光华
        @date: 2018/10/11
        本接口为修改楼，需要提供楼以前的名字，还有要改的名字
        提交方式: POST
        提交数据格式:
                    {"tower_name": "xxx", "new_tower_name": "xxx"}
        @ 整理： 董海明
    """
    info = dict(request.POST)
    print(info)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ","")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        tower_num.update(tower_name=info["new_tower_name"][0])
        return HttpResponse(json.dumps({"message": "楼名修改成功"}))


@verify_control
def change_floor_api(request):
    """
        @auther: 辛光华
        @date: 2018/10/11
        本接口为修改楼层，需要判断楼名还有原楼层名
        提交方式: POST
        提交数据格式: { "tower_name":"xxx", "floor_name":"xxx", "new_floor_name":"xxx"}
        @ 整理： 董海明
        """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        try:
            floor_name = tower_num.floor_set.get(floor_num=info["floor_name"][0]).replace(" ", "")
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "输入的楼层不存在，请重新输入"}))
        else:
            floor_name.update(tower_name=info["new_floor_name"][0])
            return HttpResponse(json.dumps({'message': "楼层信息修改成功"}))


@verify_control
def change_room_api(request):
    """
        @auther: 辛光华
        @date: 2018/10/11
        本接口为修改房间接口，需要判断楼、楼层、原房间名，根椐所提供内容修改房间名
        提交方式: POST
        提交数据格式: { "tower_name":"xxx", "floor_name":"xxx", "room_name":"xxx", "new_room_name":"xxx"}
    """

    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        try:
            floor_name = tower_num.floor_set.get(floor_num=info["floor_name"][0]).replace(" ", "")
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "输入的楼层不存在，请重新输入"}))
        else:
            try:
                room_name = floor_name.room_set.get(room_num=info["room_name"][0]).replace(" ", "")
            except Room.DoesNotExist:
                return HttpResponse(json.dumps({"message": "输入的房间名不存在，请重新输入"}))
            else:
                room_name.update(room_num=info["new_room_name"][0])
                return HttpResponse(json.dumps({"message": "房间信息修改成功"}))


@verify_control
def inquire_tower_api(request):
    """
        @author:王江萌
        @date:2018/10/12
        本接口为查询楼， 提供在数据库内查找要录入的楼是否已经存在
        提交方式 ：POST
        提交数据格式 ：{'tower_name':'xxx'}
        @ 整理： 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0])
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        return HttpResponse(json.dumps({"message": "您要查找的楼的名字是{}，ID为{}".format(tower_num.tower_name, tower_num.id)}))


@verify_control
def inquire_floor_api(request):
    """
        @author:王江萌
        @date:2018/10/12
        本接口为楼层查询接口，输入楼号，查询所有的楼层
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx"}
        @ 整理： 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        floor_list = tower_num.floor_set.all()
        return HttpResponse(json.dumps({"message": floor_list}))


@verify_control
def inquire_room_api(request):
    """
        @author:王江萌
        @date:2018/10/12
        本接口为查询房间接口，根据提供的信息，查询出所有的房间
        提交方式 ：POST
        提交数据格式 ：{"tower_name":"xxx", "floor_name":"xxx"}
        @ 整理： 董海明
    """
    info = dict(request.POST)
    try:
        tower_num = Tower.objects.get(tower_name=info["tower_name"][0]).replace(" ", "")
    except Tower.DoesNotExist:
        return HttpResponse(json.dumps({"message": "输入的楼名不存在，请重新输入"}))
    else:
        try:
            floor_name = tower_num.floor_set.get(floor_num=info["floor_num"][0]).replace(" ", "")
        except Floor.DoesNotExist:
            return HttpResponse(json.dumps({"message": "输入的楼层不存在，请重新输入"}))
        else:
            room_list = floor_name.room_set.all()
            return HttpResponse(json.dumps({"message": room_list}))




